package com.renwei.yunlong.activity.me;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.InformationBean;
import com.renwei.yunlong.event.ChatCloseEvent;
import com.renwei.yunlong.event.ChatListCloseEvent;
import com.renwei.yunlong.http.Api;
import com.renwei.yunlong.http.HttpPresenter;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.CommonUtils;
import com.renwei.yunlong.utils.LogUtil;
import com.renwei.yunlong.utils.StringUtils;
import com.renwei.yunlong.view.RatingBar;
import com.renwei.yunlong.view.RoundImageView;
import com.renwei.yunlong.view.SimpleOptionView;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;
import io.rong.imkit.RongIM;
import io.rong.imlib.model.Conversation;

/**
 * 个人信息 展示页面
 */
public class DisplayUserInfoActivity extends BaseActivity implements View.OnClickListener {


    @Bind(R.id.simpleTileView)
    SimpleOptionView simpleTileView;
    @Bind(R.id.bt_chat)
    Button btChat;
    @Bind(R.id.iv_head)
    RoundImageView ivHead;
    @Bind(R.id.tv_nickname)
    TextView tvNickname;
    @Bind(R.id.rtb_star_fives)
    RatingBar rtbStarFives;
    @Bind(R.id.tv_star_count)
    TextView tvStarCount;
    @Bind(R.id.rl_nickName)
    LinearLayout rlNickName;
    @Bind(R.id.iv_next)
    ImageView ivNext;
    @Bind(R.id.tv_work_number)
    TextView tvWorkNumber;
    @Bind(R.id.rl_click_replace)
    RelativeLayout rlClickReplace;
    @Bind(R.id.tv_sex_text)
    TextView tvSexText;
    @Bind(R.id.tv_sex)
    TextView tvSex;
    @Bind(R.id.rl_sex)
    RelativeLayout rlSex;
    @Bind(R.id.tv_birthday_text)
    TextView tvBirthdayText;
    @Bind(R.id.tv_birthday)
    TextView tvBirthday;
    @Bind(R.id.rl_born)
    RelativeLayout rlBorn;
    @Bind(R.id.tv_department_text)
    TextView tvDepartmentText;
    @Bind(R.id.tv_department_name)
    TextView tvDepartmentName;
    @Bind(R.id.rl_department)
    RelativeLayout rlDepartment;
    @Bind(R.id.tv_userGroup_text)
    TextView tvUserGroupText;
    @Bind(R.id.tv_userGroup_name)
    TextView tvUserGroupName;
    @Bind(R.id.rl_grouping)
    RelativeLayout rlGrouping;
    @Bind(R.id.tv_employee_text)
    TextView tvEmployeeText;
    @Bind(R.id.tv_employee_code)
    TextView tvEmployeeCode;
    @Bind(R.id.rl_job_number)
    RelativeLayout rlJobNumber;
    @Bind(R.id.tv_role_text)
    TextView tvRoleText;
    @Bind(R.id.tv_role_name)
    TextView tvRoleName;
    @Bind(R.id.rl_character)
    RelativeLayout rlCharacter;
    @Bind(R.id.tv_status_text)
    TextView tvStatusText;
    @Bind(R.id.tv_state)
    TextView tvState;
    @Bind(R.id.rl_status)
    RelativeLayout rlStatus;
    @Bind(R.id.tv_mobile_text)
    TextView tvMobileText;
    @Bind(R.id.tv_mobile_Phone)
    TextView tvMobilePhone;
    @Bind(R.id.rl_phone)
    RelativeLayout rlPhone;
    @Bind(R.id.tv_office_telephone_title)
    TextView tvOfficeTelephoneTitle;
    @Bind(R.id.tv_office_telephone)
    TextView tvOfficeTelephone;
    @Bind(R.id.rl_office_phone)
    RelativeLayout rlOfficePhone;
    @Bind(R.id.tv_email_text)
    TextView tvEmailText;
    @Bind(R.id.tv_email)
    TextView tvEmail;
    @Bind(R.id.rl_email)
    RelativeLayout rlEmail;
    @Bind(R.id.tv_special_text)
    TextView tvSpecialText;
    @Bind(R.id.tv_special_name)
    TextView tvSpecialName;
    @Bind(R.id.rl_special)
    RelativeLayout rlSpecial;
    @Bind(R.id.iv_certificate_text)
    TextView ivCertificateText;
    @Bind(R.id.iv_certificate_icon)
    ImageView ivCertificateIcon;
    @Bind(R.id.tv_certificate_name)
    TextView tvCertificateName;
    @Bind(R.id.rl_certificate)
    RelativeLayout rlCertificate;
    @Bind(R.id.rl_chat)
    RelativeLayout rlChat;
    private String employeeId;
    private InformationBean.Rows bean;
    private String sourceFlagCode;
    private String thisCompanyType = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_user_info);
        ButterKnife.bind(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);
        employeeId = getIntent().getStringExtra("employeeId");
        sourceFlagCode = getIntent().getStringExtra("sourceFlagCode");

        simpleTileView.setTitle("人员信息");
        rlCertificate.setOnClickListener(this);
        btChat.setOnClickListener(this);
        initData();
    }

    public void initData() {
        getThisCompanyType();
        getNormalUserData();
    }

    private void getNormalUserData() {
        HttpPresenter hp = new HttpPresenter(this);
        if ("1".equals(thisCompanyType)) {
            hp.setUrl(Api.$().OWNER_EMPLOYEE_URL);
        } else if ("2".equals(thisCompanyType)) {
            hp.setUrl(Api.$().SERVICE_EMPLOYEE_URL);
        }
        hp.setshowDialogcontent("正在加载");
        String jsonBean = "{employeeId:\"" + employeeId + "\"}";

        hp.putParam("jsonBean", jsonBean);
        hp.setCallBack(new HttpTaskListener() {
            @Override
            public void onSuccess(int requestId, String result) {
                InformationBean bean = new Gson().fromJson(result, InformationBean.class);
                if (bean.getMessage().getCode() == 200) {
                    if (bean.getRows() != null && !TextUtils.isEmpty(bean.getRows().getUserId())) {
                        setData(bean.getRows());
                    } else {
                        sourceFlagCode = "1";
                        initData();
                    }
                } else {
                    showCenterInfoMsg(bean.getMessage().getMessage());
                }
            }

            @Override
            public void onException(int requestId, String code) {
                LogUtil.e("查看人员信息错误:" + code);
            }
        });
        hp.post();
    }

    public void setData(InformationBean.Rows bean) {
        this.bean = bean;
        String userEmployeeId = "";
        if ("1".equals(companyType)) {
            userEmployeeId = ownerBean.getRows().getEmployeeId();
        } else if ("2".equals(companyType)) {
            userEmployeeId = serviceLoginBean.getRows().getEmployeeId();
        }
        if (!"1".equals(bean.getState()) || userEmployeeId.equals(employeeId)) {
            rlChat.setVisibility(View.GONE);
        } else {
            rlChat.setVisibility(View.VISIBLE);
        }
        Glide.with(this).load("1".equals(thisCompanyType) ? Api.$().OWNER_FILE_URL + bean.getAvatar() : Api.$().SERVICE_FILE_URL + bean.getAvatar())
                .error(R.mipmap.icon_header_default).into(ivHead);
        tvNickname.setText(bean.getName());
        tvWorkNumber.setText("已解决工单  " + StringUtils.value(bean.getWorkOrderNumber()));
        if ("0".equals(bean.getGender())) {
            tvSex.setText("女");
        } else if ("1".equals(bean.getGender())) {
            tvSex.setText("男");
        }

        if (!TextUtils.isEmpty(bean.getBirthday())) {
            tvBirthday.setText(bean.getBirthday().split("T")[0]);
        }
        tvDepartmentName.setText(bean.getDepartmentName());
        tvUserGroupName.setText(bean.getUserGroupName());
        tvEmployeeCode.setText(bean.getEmployeeCode());
        tvRoleName.setText(bean.getRoleName());
        if ("1".equals(bean.getState())) {
            if ("1".equals(bean.getEngineerStatus())) {
                tvState.setText("正常");
            } else if ("2".equals(bean.getEngineerStatus())) {
                tvState.setText("挂起");
            }
        } else if ("2".equals(bean.getState())) {
            tvState.setText("删除");
        }
        tvMobilePhone.setText(bean.getMobilePhone());
        tvOfficeTelephone.setText(bean.getOfficeTelephone());
        tvEmail.setText(bean.getEmail());
        tvSpecialName.setText(bean.getTechnicalQualifName());
        rtbStarFives.setStar(Float.parseFloat(TextUtils.isEmpty(bean.getLock()) ? "0" : bean.getLock()));
        tvStarCount.setText(TextUtils.isEmpty(bean.getLock()) ? "0" : bean.getLock());
    }


    private void getThisCompanyType() {
        if (TextUtils.isEmpty(sourceFlagCode) || "0".equals(sourceFlagCode)) {
            thisCompanyType = companyType;
        } else if ("1".equals(sourceFlagCode)) {
            if ("1".equals(companyType)) {
                thisCompanyType = "2";
            } else {
                thisCompanyType = "1";
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_chat: // 聊天
                if (bean != null) {
                    creatGroup();
                }
                break;
            case R.id.rl_certificate:
                Intent intent = new Intent(this, UserCertificateActivity.class);
                intent.putExtra("from", "friend");
                intent.putExtra("thisCompanyType", thisCompanyType);
                intent.putExtra("employeeId", employeeId);
                LogUtil.e("thisCompanyType", thisCompanyType);
                startActivity(intent);
                break;
            default:
                break;
        }
    }


    private void creatGroup() {
        EventBus.getDefault().post(new ChatListCloseEvent());
        EventBus.getDefault().post(new ChatCloseEvent());
        Bundle bundle = new Bundle();
        bundle.putString("url", "1".equals(thisCompanyType) ? Api.$().OWNER_FILE_URL + bean.getAvatar() : Api.$().SERVICE_FILE_URL + bean.getAvatar());
        RongIM.getInstance().startConversation(mContext, Conversation.ConversationType.PRIVATE, bean.getEmployeeId(), bean.getName(), bundle);

    }


    /**
     * @param context
     * @param employeeId
     * @param sourceFlagCode 外协公司code  没有就不传
     */
    public static void OpenActivity(Context context, String employeeId, String sourceFlagCode) {
        Intent intent = new Intent(context, DisplayUserInfoActivity.class);
        intent.putExtra("employeeId", employeeId);
        intent.putExtra("sourceFlagCode", sourceFlagCode);
        context.startActivity(intent);
    }
}
